// JavaScript Document
var current_page = 1;
var total_pages;
var plugin_path;


jQuery(function(){
	//jQuery("a" ).click(function() { alert('hi'); return false; });
	
	  		/* JQ UI button */
	  		//jQuery( "input:submit, button" ).button();
			
			
			
			//hiding all convo but only page-1
			
			jQuery("#nm-convo-container ul li.nm-c-p-1").show();
			
	  	
	  		jQuery("#inbox, #compose").click(function(){
				
			/* hiding panel*/
			jQuery("#inbox-panel, #compose-panel, #convo-history-panel").hide();
		
			/* what to show */
			jQuery("#"+this.id+"-panel").slideDown();
			
		});		
			
		//select all 
		jQuery("#convo-select-all").click(function(){
			//alert(current_page);
			jQuery("ul.nm-c-p-"+current_page).find(':checkbox').attr('checked', this.checked);
			
		});
		
		/* auto complate */
		jQuery( "#tags" ).autocomplete({
			source: scriptAr
		});

		
		/* auto complete using jq ui*/
		
		
});

/*
This is loading message history
*/
function loadConvoHistory(c_id)
{
	//alert(url_convo_detail);
	//hiding other stuff
	jQuery("#inbox-panel, #compose-panel").hide();
	
	//but showing me
	jQuery("#convo-history-panel").show();
	
	//setting title of pop up 
	var t = jQuery("#convo-"+c_id).find("li.title").html();
	
	//binding convo id value to reply form hidden id field
	jQuery("#reply-c-id").val(c_id);
	
	jQuery("#convo-detail-container").load(url_convo_detail, {dirpath:dir_path, cid:c_id});
	jQuery("#history-heading").html(t);
	
	//mark as read
	markAsRead(c_id);
}

/* mark convo as read */
function markAsRead(cid)
{
	//alert(plugin_path);
	var smart = 'http://192.168.1.13/wordpress/wp-content/plugins/nm-member-convo/smart.php';
	jQuery.post(smart, {convo_id : cid}, function(data)
	{
		//alert(data);
	});
}

/* pagination */
function loadConvoPageNext()
{
	jQuery("#nm-convo-container ul li.nm-c-p-"+current_page++).hide();
	jQuery("#nm-convo-container ul li.nm-c-p-"+current_page).show();
	setPagination();
}

function loadConvoPagePrev()
{
	jQuery("#nm-convo-container ul li.nm-c-p-"+current_page--).hide();
	jQuery("#nm-convo-container ul li.nm-c-p-"+current_page).show();
	setPagination();
}


function loadConvoCurrentPage()
{
	
	//showing inbox panel
  	jQuery("#inbox-panel").show();
	
	//hiding history/detail panel me
	jQuery("#convo-history-panel").hide();
	
	//loading current page
	
	jQuery("#nm-convo-container ul li.nm-c-p-"+current_page).show();
	setPagination();
	
	
}

function setPagination()
{
	if(total_pages == 1)
	{
		jQuery("#prev-page a").hide();
		jQuery("#next-page a").hide();
	}
	else if(total_pages == current_page)
	{
		jQuery("#next-page a").hide();
		jQuery("#prev-page a").show();
	}
	else if(total_pages > current_page && current_page > 1)
	{
		jQuery("#prev-page a").show();
	}
	else
	{
		jQuery("#prev-page a").hide();
		jQuery("#next-page a").show();
	}	
	
	//setting page couner lable
	jQuery("#page-count").html(current_page+" of "+total_pages);
	
	// uncheck top delete 
	jQuery("#convo-select-all").attr('checked', false);
}


/* new message validation */
function validateCompose(){
		error=0;
		if(jQuery("#tags").val()==""){
			 jQuery("#tags").css("border-color", "red");
			jQuery("#start_with_err").show().fadeOut(10000);
			error=1;
		} 

		if(jQuery("#subject").val()==""){
			jQuery("#subject").css("border-color", "red");
			jQuery("#subject_err").show().fadeOut(11000);
			error=1;
		} 
		
		if(jQuery("#message").val()==""){
			jQuery("#message").css("border-color", "red");
			jQuery("#message_err").show().fadeOut(12000);
			error=1;
		} 
		
		if(error==1) return false; else return true;
		
}


/* reply validation */
function validateReply(){
		error=0;
		
		if(jQuery("#nm-reply").val()==""){
			jQuery("#nm-reply").css("border-color", "red");
			jQuery("#reply_err").show().fadeOut(12000);
			error=1;
		} 
		
		if(error==1) return false; else return true;
		
}